package com.hzqy.web.ac.epg;


import com.alibaba.fastjson.JSONArray;
import com.hzqy.commons.utils.ExcelUtil;
import com.hzqy.commons.utils.ExcelUtil.MyEntry;
import com.hzqy.commons.utils.FileType;
import com.hzqy.service.epg.AcPhoneBillService;
import com.hzqy.web.vo.epg.AcPhoneBillVo;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/acPhoneBill")
public class AcPhoneBillAction {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	private static final String[] allowedFileType = new String[]{"png", "jpg", "gif", "zip", "jar", "tar", "docx", "doc", "xlsx", "xls", "flv", "mp4", "mp3", "mpg", "avi", "mov", "wmv"};
    @Resource
	private AcPhoneBillService serviceAction;
    @RequestMapping("/acPhoneBillSelectAll")
	@ResponseBody
	public Object acSignSelect(AcPhoneBillVo apb, int page, int rows){
		Map<String,Object> SignListMap = new HashMap<String, Object>();
		SignListMap= serviceAction.selectAc(apb,page, rows);
		return SignListMap;
	}
	@RequestMapping("/acPhoneBillExcel")
	@ResponseBody
	public void acPhoneBillExcel(AcPhoneBillVo infoVo, HttpSession session, int page, int rows, HttpServletResponse response) throws IOException {
		Map<String,Object> ListMap = new HashMap<String, Object>();
    	ListMap = serviceAction.selectAc(infoVo,1,100000);
		List<AcPhoneBillVo> data=(List<AcPhoneBillVo>)ListMap.get("rows");
		List<MyEntry<String, String>> excelHeader=new ArrayList<MyEntry<String, String>>();
		excelHeader.add(new MyEntry<String,String>("f_apb_id","ID"));
		excelHeader.add(new MyEntry<String,String>("f_apb_cardnumber","话费卡号"));
		excelHeader.add(new MyEntry<String,String>("f_apb_cardkey","卡密"));
		excelHeader.add(new MyEntry<String,String>("f_apb_user","用戶itv"));
		excelHeader.add(new MyEntry<String,String>("f_apb_effectivetime","有效期"));
		excelHeader.add(new MyEntry<String,String>("f_apb_price","价格(分为单位)"));
		excelHeader.add(new MyEntry<String,String>("f_apb_status","有效状态"));
		excelHeader.add(new MyEntry<String,String>("f_apb_cdate","存入时间"));
		excelHeader.add(new MyEntry<String,String>("f_apb_udate","使用时间"));
		boolean flag= ExcelUtil.generateExcel(data, excelHeader, response);
	}
/*	@RequestMapping("/importPhoneBill")
	@ResponseBody
	public Object insertBatchphoneBill(@RequestParam(value = "uploadify") MultipartFile file ,HttpServletResponse response) throws Exception {
		if (FileType.checkFileType(file.getInputStream(), allowedFileType)) {
			int successCount = 0;
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Calendar c = new GregorianCalendar(1900, 0, -1);
			JSONArray emptyArr = new JSONArray();
			StringBuffer str = new StringBuffer();
			StringBuffer str2 = new StringBuffer();
			Date date;
			InputStream is = null;
			try {
				LOG.debug("批量导入phoneBill开始");
				is = file.getInputStream();
				List<Map<String, Object>> list = ExcelUtil.parseExcel(is, ExcelUtil.ALLOWED_EMPTY_MODE, new String[]{"卡号", "卡密", "价格", "有效期"});
				LOG.debug("解析excel完成, 开始循环入库");
				for (int i = 0; i < list.size(); i++) {
					AcPhoneBillVo phoneBill = new AcPhoneBillVo();
					Map<String, Object> map = list.get(i);
					phoneBill.setF_apb_cardnumber(map.get("卡号").toString());
					phoneBill.setF_apb_cardkey(map.get("卡密").toString());
					phoneBill.setF_apb_price(map.get("价格").toString());
					//date= DateUtils.addDays(c.getTime(),map.get("有效期").hashCode());
					phoneBill.setF_apb_user("");
					phoneBill.setF_apb_effectivetime(map.get("有效期").toString());
					//phoneBill.setF_apb_effectivetime(format.format(date).substring(0,10));
					boolean success = false;
					Map<String, Object> resultMap = serviceAction.insertPhoneBillService(phoneBill);
					success = (Boolean) resultMap.get("success");
					successCount += (success ? 1 : 0);
				}
				LOG.debug("批量完成" + successCount);
				str.append("一共").append(list.size()).append("条数据").append("批量导入完成,：").append(successCount).append("条数据成功").append(list.size() - successCount).append("条失败");
			} catch (Exception e) {
				LOG.error("", e);
				str.append("批量导入失败: ");
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
					}
				}
			}
			return str.toString();
		} else {
			return null;
		}
	}*/

}
